// pages/index/index.js
import {
  getUserAdopt
} from '../../utils/adopt/adopt'
import {
  getGood,
  addUserGood,
  getUserGood,
  editUserGood
} from '../../utils/good/good'
import getOpenID from '../../utils/getOpenID'
import {
  canFeed,
  setFeedInfo
} from '../../utils/feed/feed'

let timer = null

Page({
  /**
   * 页面的初始数据
   */
  data: {
    hasAdopt: false,
    swiperIndex: 0,
    userAdoptList: [],
    userGoodList: {},
    goodList: []
  },
  //  点击喂养的业务逻辑
  async onFeed() {
    wx.showToast({
      title: `投喂中...`,
      icon: 'loading',
      duration: 30000
    })
    const {
      swiperIndex,
      userAdoptList
    } = this.data
    const goodId = userAdoptList[swiperIndex].good
    const userAdoptId = userAdoptList[swiperIndex].id
    const [flag, nextTime] = await canFeed(userAdoptId)

    if (flag) {
      await Promise.all([setFeedInfo(userAdoptId), addUserGood(goodId)])
      wx.showToast({
        title: `喂养成功！`,
      })
      const userGoodList = await getUserGood()
      this.setData({
        userGoodList
      })
    } else {
      //   toast 吐司
      wx.showToast({
        title: `还没有到喂食时间，还需${ ~~((nextTime - Date.now())/1000) }秒`,
        icon: 'none'
      })
    }

  },
  async onHarveUserGood(event) {
    await editUserGood(event.detail)
    wx.showToast({
      title: '成功收取商品',
    })
    const userGoodList = await getUserGood()
    this.setData({
      userGoodList
    })
  },
  async onDisableUserGood(event){
    await editUserGood(event.detail)
    wx.showToast({
      title: '已清除过期商品',
    })
    const userGoodList = await getUserGood()
    this.setData({
      userGoodList
    })
  },
  //  监听是否有用户商品 子组件 过期了
  onTimeOut() {
    clearTimeout(timer)

    timer = setTimeout(() => {
      console.log('timeout')
      this.setData({
        userGoodList: {...this.data.userGoodList}
      })
    }, 200)
 
  },
  // 轮播图 切换时触发的事件回调
  onSwiperChange({
    detail: {
      current
    }
  }) {
    this.setData({
      swiperIndex: current
    })
  },
  // 点击领养按钮后的回调，实现页面跳转
  onToAdopt(event) {
    console.log(event)
    // wx 全局实例
    wx.navigateTo({
      url: '/pages/adopt/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // 获取全局的app实例
    // const app = getApp()
    // console.log(app.globalData)
    const res = await getUserGood()
    const goodList = await getGood()
    this.setData({
      userGoodList: res,
      goodList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    const data = await getUserAdopt()
    // console.log(data)
    this.setData({
      userAdoptList: data
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})